package com.szsfm.vehiclemanagement.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;

import com.kyleduo.switchbutton.SwitchButton;
import com.szsfm.vehiclemanagement.R;
import com.szsfm.vehiclemanagement.base.BaseFragment;
import com.szsfm.vehiclemanagement.manager.SharePrefManager;

/**
 * Created by zhoubigbo on 2017/11/8.
 */
public class SettingFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener,View.OnClickListener {

    SwitchButton mNewMessageButton;
    SwitchButton mSoundButton;
    SwitchButton mVibrateButton;
    Button mLogoutButton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_setting, container, false);

        mNewMessageButton = contentView.findViewById(R.id.remind_new_msg_swb);
        mSoundButton = contentView.findViewById(R.id.remind_new_sound_swb);
        mVibrateButton = contentView.findViewById(R.id.remind_new_vibrate_swb);
        mNewMessageButton.setOnCheckedChangeListener(this);
        mSoundButton.setOnCheckedChangeListener(this);
        mVibrateButton.setOnCheckedChangeListener(this);
        mLogoutButton = contentView.findViewById(R.id.log_out_button);
        mLogoutButton.setOnClickListener(this);

        mNewMessageButton.setChecked(SharePrefManager.isNewMessageSwitchOn());
        mSoundButton.setChecked(SharePrefManager.isSoundSwitchOn());
        mVibrateButton.setChecked(SharePrefManager.isVibrateSwitchOn());

        return contentView;
    }

    @Override
    public void onCheckedChanged(CompoundButton button, boolean on) {
        if (button == mNewMessageButton) {
            SharePrefManager.setNewMessageSwitchOn(on);
            if (!on){
                mSoundButton.setChecked(false);
                mVibrateButton.setChecked(false);
            }
        } else if (button == mSoundButton) {
            SharePrefManager.setSoundSwitchOn(on);
        } else if (button == mVibrateButton) {
            SharePrefManager.setVibrateSwitchOn(on);
        }
    }

    @Override
    public void onClick(View view) {
        getActivity().onBackPressed();
    }
}
